package com.im.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.im.db.DBFactory;
import com.im.entity.Car;
import com.im.entity.Member;

public class CarDAO {
	
	public static String insert(Car car){
		try{
			String sql = "INSERT INTO car(title, description,brand,model,price,image) VALUES (?,?,?,?,?,?)";
			
			Connection con = DBFactory.getCon();
			PreparedStatement ps = con.prepareStatement(sql);
	
			ps.setString(1, car.getTitle());
			ps.setString(2, car.getDescription());
			ps.setString(3, car.getBrand());
			ps.setString(4, car.getModel());
			ps.setDouble(5, car.getPrice());
			ps.setString(6, car.getImage());
			
			ps.executeUpdate();
			
			return "Posting Successfull !";
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "please retry!";
	}
	
	public static List<Car> listCars(){
		List<Car> list = new ArrayList<Car>();
		
		try{
			String sql = "SELECT * FROM car ";
			
			Connection con = DBFactory.getCon();
			PreparedStatement ps = con.prepareStatement(sql);
			
			ResultSet rs = ps.executeQuery();
			
			while(rs.next()){
				Car car = new Car();
				car.setId(rs.getInt("id"));
				car.setTitle(rs.getString("title"));
				car.setDescription(rs.getString("description"));
				car.setBrand(rs.getString("brand"));
				car.setModel(rs.getString("model"));
				car.setPrice(rs.getDouble("price"));
				car.setImage(rs.getString("image"));
				list.add(car);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return list;
	}
}
